﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "LineTypeButton.h"
#include "ILine3d.h"
#include "IUiView.h"
#include "Vector3d.h"
#include "GbmpDefines.h"
#include "IUiDocument.h"
#include "ILineTypeNew.h"
#include "ILineTypeData.h"
#include "IUiDocumentViewManager.h"

#include <QPainter>
#include <QStyleOptionButton>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

/// 本文件是界面实现的辅助类，不包含GDMP API的演示内容

using namespace gcmp;

namespace Sample
{
    LineTypeButton::LineTypeButton(QWidget *parent)
        : QPushButton(parent)
        , m_lineTypeName(L"")
        , m_lineWidth(1)
    {

    }

    void LineTypeButton::SetLineTypeName(const std::wstring& lineTypeName)
    {
        m_lineTypeName = lineTypeName;
    }
    const std::wstring& LineTypeButton::GetLineTypeName() const
    {
        return m_lineTypeName;
    }

    void LineTypeButton::SetLineWidth(int lineWidth)
    {
        m_lineWidth = lineWidth;
    }
    int LineTypeButton::GetLineWidth() const
    {
        return m_lineWidth;
    }

    void LineTypeButton::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        QStyleOptionButton opt;
        opt.initFrom(this);
        style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
        style()->drawItemText(&painter, opt.rect, Qt::AlignCenter, opt.palette, true, text());

        std::wstring lineTypeName = m_lineTypeName;
        if (m_lineTypeName == L"")
        {
            lineTypeName = L"StraightLine";
        }

        painter.save();
        QPen pen;
        pen.setColor(Qt::black);
        pen.setWidth(m_lineWidth);
        painter.setPen(pen);

        // 构造待离散的预览直线
        double height = (rect().top() + rect().bottom()) * 0.5;
        OwnerPtr<ILine3d> opLineToDiscrete = ILine3d::Create(Vector3d(rect().left() + 20.0, height, 0.0), Vector3d(rect().right() - 20.0, height, 0.0));
        std::vector<Vector3d> vertexList, pointList;

        IUiDocumentViewManager* pUiDocumentViewManager = IUiDocumentViewManager::Get();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDocumentViewManager, L"pUiDocumentViewManager为空",L"GDMP",L"2024-03-30");
        IUiView* pUIView = pUiDocumentViewManager->GetCurrentUiView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUIView, L"pUIView为空",L"GDMP",L"2024-03-30");
        IUiDocument* pUIDoc = pUIView->GetUiDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc, L"pUIDoc为空",L"GDMP",L"2024-03-30");
        IDocument* pDoc = pUIDoc->GetDbDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空",L"GDMP",L"2024-03-30");
        const ILineTypeNew* pLineType = ILineTypeNew::GetLineTypeByName(pDoc, lineTypeName);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLineType, L"没找到对应线型",L"GDMP",L"2024-03-30");
        const ILineTypeData* plnTypeData = pLineType->GetLineTypeData();
        DBG_WARN_AND_RETURN_VOID_UNLESS(plnTypeData, L"ILineTypeData为空",L"GDMP",L"2024-03-30");
        plnTypeData->Discrete(opLineToDiscrete.get(), -1.0, vertexList, pointList);

        if (!vertexList.empty())
        {
            for (int i = 0; i < (int)vertexList.size() - 1;)
            {
                painter.drawLine(QPointF(vertexList[i].X(), vertexList[i].Y()), QPointF(vertexList[i + 1].X(), vertexList[i + 1].Y()));
                i += 2;
            }
        }

        QVector<QPointF> qPointList;
        for each (auto& itr in pointList)
        {
            qPointList.push_back(QPointF(itr.X(), itr.Y()));
        }
        painter.drawPoints(qPointList);
        painter.restore();
    }
}

